/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

/**
 *
 * @author David Harrison
 * 3 May 2008
 *
 */

import com.sfs.postcodes.beans.GeocodeBean;
import com.sfs.postcodes.dao.GeocoderDAO;
import com.sfs.postcodes.dao.PostcodeDAO;

import java.util.StringTokenizer;

import net.sourceforge.stripes.integration.spring.SpringBean;


/**
 * The Class LookupBaseActionBean.
 */
public abstract class LookupBaseActionBean extends BaseActionBean {

    /** The address. */
    private String address = "";

    /** The state. */
    private String state = "";

    /** The country. */
    private String country = "";

    /** The postcode. */
    private String postcode = "";

    /** The geocode. */
    private GeocodeBean geocode;

    /** The postcode dao. */
    private PostcodeDAO postcodeDAO;

    /** The geocoder dao. */
    private GeocoderDAO geocoderDAO;


    /**
     * Setter to allow the PostcodeDAO to be injected.
     *
     * @param postcodeDAORef the postcode dao ref
     */
    @SpringBean("postcodeDAO")
    public final void injectPostcodeDAO(final PostcodeDAO postcodeDAORef) {
        this.postcodeDAO  = postcodeDAORef;
    }

    /**
     * Setter to allow the GeocoderDAO to be injected.
     *
     * @param geocoderDAORef the geocoder dao
     */
    @SpringBean("geocoderDAO")
    public final void injectGeocoderDAO(final GeocoderDAO geocoderDAORef) {
        this.geocoderDAO  = geocoderDAORef;
    }

    /**
     * Gets the PostcodeDAO.
     *
     * @return the PostcodeDAO
     */
    protected final PostcodeDAO getPostcodeDAO() {
        return this.postcodeDAO;
    }

    /**
     * Gets the GeocoderDAO.
     *
     * @return the GeocoderDAO
     */
    protected final GeocoderDAO getGeocoderDAO() {
        return this.geocoderDAO;
    }

    /**
     * Sets the address.
     *
     * @param addressValue the new address
     */
    public final void setAddress(final String addressValue) {
        this.address = addressValue;
    }

    /**
     * Gets the address.
     *
     * @return the address
     */
    public final String getAddress() {
        return this.address;
    }

    /**
     * Gets the formatted address.
     *
     * @return the formatted address
     */
    public final String getFormattedAddress() {
        return convertAddress(", ");
    }

    /**
     * Gets the html address.
     *
     * @return the html address
     */
    public final String getHtmlAddress() {
        return convertAddress("<br/>");
    }

    /**
     * Sets the state.
     *
     * @param stateValue the new state
     */
    public final void setState(final String stateValue) {
        this.state = stateValue;
    }

    /**
     * Gets the state.
     *
     * @return the state
     */
    public final String getState() {
        return this.state;
    }

    /**
     * Sets the country.
     *
     * @param countryValue the new country
     */
    public final void setCountry(final String countryValue) {
        this.country = countryValue;
    }

    /**
     * Gets the country.
     *
     * @return the country
     */
    public final String getCountry() {
        return this.country;
    }

    /**
     * Sets the postcode.
     *
     * @param postcodeValue the new postcode
     */
    protected final void setPostcode(final String postcodeValue) {
        this.postcode = postcodeValue;
    }

    /**
     * Gets the postcode.
     *
     * @return the postcode
     */
    public final String getPostcode() {
        return this.postcode;
    }

    /**
     * Gets the postcode set.
     *
     * @return the postcode set
     */
    public final boolean getPostcodeSet() {
        boolean exists = false;
        if (postcode != null && postcode.compareTo("") != 0) {
            exists = true;
        }
        return exists;
    }

    /**
     * Sets the geocode.
     *
     * @param geocodeRef the new geocode
     */
    protected final void setGeocode(final GeocodeBean geocodeRef) {
        this.geocode = geocodeRef;
    }

    /**
     * Gets the geocode.
     *
     * @return the geocode
     */
    public final GeocodeBean getGeocode() {
        return this.geocode;
    }

    /**
     * Gets the geocode set.
     *
     * @return the geocode set
     */
    public final boolean getGeocodeSet() {
        boolean exists = false;
        if (geocode != null) {
            exists = true;
        }
        return exists;
    }

    /**
     * Convert address.
     *
     * @param lineBreak the line break
     *
     * @return the string
     */
    private String convertAddress(final String lineBreak) {
        StringBuffer convertedAddress = new StringBuffer();
        if (this.address != null) {
            StringTokenizer tk = new StringTokenizer(this.address, "\n");
            while (tk.hasMoreTokens()) {
                String token = tk.nextToken();
                if (convertedAddress.length() > 0) {
                    convertedAddress.append(lineBreak);
                }
                convertedAddress.append(token.trim());
            }
        }
        return convertedAddress.toString();
    }
}
